package com.line.config.exception;

import com.line.config.cons.CodeCons;
import com.line.model.pojo.ErrorResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalControllerAdvice {

	private Logger log = LoggerFactory.getLogger(GlobalControllerAdvice.class);

	@ResponseBody
	@ExceptionHandler(value = Exception.class)
	public ErrorResponse exceptionHandler(Exception ex) {
		log.error("接口未知异常：", ex);
		return new ErrorResponse(CodeCons.ERROR);
	}

	@ResponseBody
	@ExceptionHandler(value = GlobalResponseException.class)
	public ErrorResponse myExceptionHandler(GlobalResponseException exception) {
		ErrorResponse response = new ErrorResponse(exception.getCodeCons());
		// 发生异常进行日志记录，写入数据库或者其他处理，此处省略
		log.info("Error Code:{}, Msg:{}", response.getErrcode(), response.getErrmsg());
		return response;
	}
}
